package main

import (
	"context"
	"fmt"
	"grpc-go-demo/echo"
	"log"
	"net"

	"google.golang.org/grpc"
)

type EchoImpl struct {
	echo.UnimplementedEchoServer
}

func (e *EchoImpl) UnaryEcho(ctx context.Context, req *echo.EchoRequest) (*echo.EchoResponse, error) {
	log.Println("received: ", req.GetMessage())
	response := &echo.EchoResponse{Message: req.GetMessage()}
	return response, nil
}

func main() {
	listener, err := net.Listen("tcp", ":8080")
	if err != nil {
		log.Fatalln("open port error: ", err.Error())
	}
	defer listener.Close()

	grpcServer := grpc.NewServer()

	echo.RegisterEchoServer(grpcServer, &EchoImpl{})

	fmt.Println("server start at port:8080")

	grpcServer.Serve(listener)
}
